<?php
/**
 * Class descript.
 * User: chan
 * Date: 2017/10/26 15:30
 */
namespace app\api\logic;
use app\api\BaseModel;
use think\Cache;
use think\Db;
use think\Request;
use app\api\validate\Login as LoginValidate;

class Login extends BaseModel
{

    /*
     *  用户注册
     */
    public function register()
    {
        return json(['data'=>123]);
        (new LoginValidate())->checkRegisterParam();
        $code = $this->requestData['code'];
        $a = '/^1[34578]{1}\d{9}$/';

            if(empty(preg_match($a,$this->requestData['mobile']))){
                return array('message'=>'手机号码有误');
            }
        if (base64_encode($code) != Cache::get('register'.$this->requestData['mobile'])) {
            return array('message'=>'验证码错误');
        }else{
            $where['mobile'] = $this->requestData['mobile'];
            $whereor['wx_openid'] = $this->requestData['openid'];
        
            $root = db('member')->where($where)->whereor($whereor)->find();
            if($root){
                return array('message'=>'手机号码已注册或该微信已注册');
            }else{
                $data['mobile'] = $this->requestData['mobile'];
                $data['sex'] = intval($this->requestData['sex']);
                $data['nickname'] = array_key_exists('nickname',$this->requestData) !== false ? addslashes($this->requestData['nickname']) : '';
                $data['wx_openid'] = $this->requestData['openid'];
                $data['province'] = isset($this->requestData['province'])?$this->requestData['province']:'';
                $data['city'] = isset($this->requestData['city'])?$this->requestData['city']:'';
                $data['county'] = isset($this->requestData['country'])?$this->requestData['country']:'';
                $data['headimage'] = $this->requestData['avatarUrl'];
                $data['register_time'] = time();

                
                if(db('member')->insert($data) === false) return array('message' => '注册失败');

                return array();
            }
        }
    }

    //修改密码
    public function update_password(){

        $code = $this->requestData['code'];
        $phone = input('phone');
        $account = input('account');
        $root = db('user_account')->where('phone',$phone)->where('account',$account)->find();

        if(!$root) return ['message'=>'账号不存在!'];
        if($root['status'] != 1) return ['message'=>'账号禁止登陆,请联系客服!'];
        if(empty($this->requestData['password'])) return ['message'=>'密码不能为空!'];

        if(Cache::get($mobile) != base64_encode($this->requestData['code'])) return array('message' => '验证码不正确错误');

        $update_date['passwd'] = password_hash($this->requestData['password'], PASSWORD_DEFAULT);

        $result = Model('Member')->updateUser($root['id'],$update_date);

        if($result['bool'] == 'false')return ['message'=>'修改失败!'];
    }
    
}